<template>
    <div id="Carrier" v-loading="loading">
        <v-tableTitle :btnConfig="btnConfig"
                      :tableSelectData="tableSelectData"
                      :title="$t('config.carrier')" @del="del" @exportFile="exportFile" @upFile="file"
                      @newCreate="newCreate"></v-tableTitle>
        <v-table
            :page="page"
            :pageSize="pageSize"
            :tableConfig="tableConfig"
            :tableData="tableData"
            :total="total"
            @findData="getTableData"
            @getTableData="getTableData"
            @searhData="searhData"
            @selectData="selectData"
            @setdialog="setdialog"
            @upData="getTableData"
            ref="table"
        ></v-table>
        <el-dialog
            :modal="false"
            :visible.sync="dialogFlag"
            title=""
            top="0"
            v-dialogDrag
            :close-on-click-modal="false"
            width="900px">
            <router-view></router-view>
        </el-dialog>
    </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import config from "./Config";

export default {
    name: "carrier",
    mixins: [baseMixin, config],
    data() {
        return {
            loading: false,
            tableConfig: [
                {
                    label: this.$t('config.carrier'),
                    prop: "storerkey",
                    input: "input",
                },
                {
                    label: this.$t('config.company'),
                    prop: "company",
                    input: "input"
                },
                {
                    label: this.$t('config.city'),
                    prop: "city",
                    input: "input",
                },
                {
                    label: this.$t('config.provice'),
                    prop: "state",
                    input: "input",
                },
                {
                    label: this.$t('config.postcode'),
                    prop: "zip",
                    input: "input",
                },
            ],
            tableData: [],
            page: 1,
            pageSize: 10,
            total: 10,
            tableSelectData: [],
            tableSearhData: {},
            dialogFlag: false
        };
    },
    computed: {
        btnConfig() {
            return [
                {label: this.$t('common.create'), certigier: this.newStatus||(!this.btnPermNameList.includes('EN02030401')&&!this.btnPermNameList.includes('WM03060401')), invalid: "newCreate"},
                {label: this.$t('common.delete'), certigier: this.deleteStatus||(!this.btnPermNameList.includes('EN02030402')&&!this.btnPermNameList.includes('WM03060402')), disabled: true, invalid: "del"},
                {type: "file1", certigier: this.newStatus||(!this.btnPermNameList.includes('EN02030403')&&!this.btnPermNameList.includes('WM03060403')), label: this.$t('common.import'), invalid: "file"},
                {label: this.$t('common.export'), certigier: !this.btnPermNameList.includes('EN02030404')&&!this.btnPermNameList.includes('WM03060404'), disabled: this.poiStatus, invalid: "exportFile"},
            ]
        }
    },
    created() {
    },
    methods: {
        async exportFile(data) {
            try {
                let res = await this.$api.post('/exportExcel/3', data, {responseType: 'blob'})
                if (window.navigator.msSaveBlob) {
                    var blobObject = new Blob([res.data]);
                    window.navigator.msSaveBlob(blobObject, this.$t('config.carrier') + '.xls');
                } else {
                    var blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'});
                    var filename = this.$t('config.carrier') + '.xls';
                    var downloadElement = document.createElement('a');
                    var href = window.URL.createObjectURL(blob); //创建下载的链接
                    downloadElement.style.display = 'none';
                    downloadElement.href = href;
                    downloadElement.download = filename; //下载后文件名
                    document.body.appendChild(downloadElement);
                    downloadElement.click(); //点击下载
                    document.body.removeChild(downloadElement); //下载完成移除元素
                    window.URL.revokeObjectURL(href); //释放掉blob对象
                }
            } catch (e) {
                this.$message.error(e);
            }
        },
        async file(data) {
            try {
                let res = await this.$api.post('/importExcel/3', data);
                
                    this.$message.success(this.$t('app.importMes'));
                    this.getTableData(1, this.tableSearhData);
                
            } catch (e) {
                this.$message.error(e);
            }
        },
        async newCreate() {
            this.$store.commit("setNewFlag", true)
            this.dialogFlag = true;
            setTimeout(() => {
                this.$router.push({name: 'carrierDeawer'});
            }, 0)
        },
        async del(data) {
            try {
                let res = await this.$api.post(`/deleteStorer/3`, data);
                
                    this.$message.success(res.returnMgr);
                    let page = sessionStorage.getItem('page');
                    this.getTableData(page, this.tableSearhData);
                    
                
            } catch (e) {
                this.$message.error(e);
            }
        },
        //选中的内容
        selectData(data) {
            this.tableSelectData = data;
        },
        async getTableData(page, data) {
            try {
                this.loading = true;
                let res = await this.$api.post(`/queryStorer/3/${page}/${this.$store.state.wms}`, data);
                
                    this.tableData = res.page.list;
                    this.page = res.page.curpage;
                    this.pageSize = res.page.pageSize;
                    this.total = res.page.total;
                
            } catch (e) {
                this.$message.error(e);
            } finally {
                this.loading = false;
            }
        },
        //搜索的值
        searhData(data, up) {
            if (!up) {
                this.tableSearhData = data;
            } else {
                this.tableSearhData = data;
                this.getTableData(1, this.tableSearhData);
            }
        },
        //设置弹框
        setdialog(data) {
            this.$store.commit("setRouteData", data);
            this.dialogFlag = true;
            setTimeout(() => {
                this.$router.push({name: 'carrierDeawer'});
            }, 0)
        },
    },
    mounted() {
        
        this.$store.commit("setSkip", {});
        this.$store.commit("setNewFlag", false);
        this.$store.commit("setRouteData", "");
        this.getTableData(1, {});

    },
    watch: {
        tableSelectData(data) {
            if (this.permissionStatus) {
                if (data.length > 0) {
                    this.deleteStatus = false;
                } else {
                    this.deleteStatus = true;
                }
            }
        },
        $route(data) {
            if (data.name === "carrier") {
                this.dialogFlag = false;
            }
        },
        dialogFlag(data) {
            if (data === false) {
                this.$store.commit("setNewFlag", false);
                this.getTableData(1, {});
                this.$router.push({name: "carrier"});
                this.$store.commit("setSkip", {});
                this.$store.commit("setRouteData", {});
            }
        }
    },
    components: {
        "v-tableTitle": tableTitle,
        "v-table": table,
    }
};
</script>

<style scoped>
#Carrier {
    width: 100%;
    height: 99%;
    padding-top: 6px;
}
</style>

